﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace umbraco.cms.presentation.create.controls
{
    using System;
    using System.Globalization;
    using System.Linq;
    using umbraco.BasePages;
    using umbraco.ACSExtensions;

    public partial class mapping : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                var nodeId = library.Request("nodeId");
                var identityProviderName = nodeId.Split('|')[1];
                var memberGroupName = nodeId.Split('|')[3];
                
                this.SubmitButton.Text = ui.Text("create");
                this.DescriptionLabel.Text = string.Format(CultureInfo.InvariantCulture, "Create mapping rule for {0} group.", memberGroupName);

                var acsManagementService = new AcsManagementService();
                var claimTypes = acsManagementService.RetrieveIdentityProviderClaimTypes(identityProviderName);
                
                if (claimTypes != null)
                {
                    this.ClaimTypes.DataSource = claimTypes;
                    this.ClaimTypes.DataBind();
                }
            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            var nodeType = library.Request("nodeType");
            var nodeId = library.Request("nodeId");
            var identityProviderId = nodeId.Split('|')[0];
            var identityProviderName = nodeId.Split('|')[1];
            var memberGroupId = nodeId.Split('|')[2];
            var memberGroupName = nodeId.Split('|')[3];

            string parameters = string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}|{3}|{4}|{5}", identityProviderId, identityProviderName, memberGroupId, memberGroupName, this.ClaimTypes.SelectedValue, this.ClaimValue.Text);
            string returnUrl = umbraco.presentation.create.dialogHandler_temp.Create(
                    nodeType,
                    int.Parse(memberGroupId, CultureInfo.InvariantCulture),
                    parameters);

            BasePage.Current.ClientTools
                .ChangeContentFrameUrl(returnUrl)
                .ChildNodeCreated()
                .CloseModalWindow();
        }
    }
}